package com.thd.springboot.framework.example.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.thd.springboot.framework.constants.CommonConstants;
import com.thd.springboot.framework.example.entity.CgExampleEntity;
import com.thd.springboot.framework.example.mapper.CgExampleMapper;
import com.thd.springboot.framework.example.service.CgExampleService;
import com.thd.springboot.framework.model.Message;
import com.thd.springboot.framework.utils.JacksonUtil;
import com.thd.springboot.framework.web.controller.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * com.thd.springboot.framework.example.controller.CgExampleController
 **/
@Controller
@RequestMapping("/cg")
public class CgExampleController extends BasicController {

    @Autowired
    private CgExampleService cgExampleServiceImpl;
    @Autowired
    private CgExampleMapper cgExampleMapper;




    @ResponseBody
    @RequestMapping("/test")
    // url : http://127.0.0.1:8899/thd/cg/test
    public Message test(){
        System.out.println("test");
        List<CgExampleEntity> l = this.cgExampleServiceImpl.queryAllCgExample();
        return Message.success(l);
    }

    @ResponseBody
    @PostMapping("/addCgExample")
    // url : http://127.0.0.1:8899/thd/cg/addCgExample
    public Message addCgExample(@RequestBody CgExampleEntity entity){
        this.cgExampleServiceImpl.create(entity);
        return Message.success("SUCCESS");
    }
    @ResponseBody
    @PostMapping("/updateCgExample")
    // url : http://127.0.0.1:8899/thd/cg/updateCgExample
    public Message updateCgExample(@RequestBody CgExampleEntity entity){
        int updateCount = this.cgExampleServiceImpl.modify(entity);
        if(updateCount!=1){
            throw new RuntimeException(" Update Failed !");
        }
        return Message.success("SUCCESS");
    }
    @ResponseBody
    @DeleteMapping("/physicsDeleteCgExample/{id}")
    // url : http://127.0.0.1:8899/thd/cg/physicsDeleteCgExample/15
    public Message physicsDeleteCgExample(@PathVariable String id){
        this.cgExampleServiceImpl.deletePhysicsById(id);
        return Message.success("SUCCESS");
    }


    @ResponseBody
    @DeleteMapping("/logicDeleteCgExample/{id}")
    // url : http://127.0.0.1:8899/thd/cg/logicDeleteCgExample/15
    public Message logicDeleteCgExample(@PathVariable String id){
        this.cgExampleServiceImpl.deleteLogicById(id);
        return Message.success("SUCCESS");
    }




    @ResponseBody
    @RequestMapping("/queryCgExampleById/{id}")
    // url : http://127.0.0.1:8899/thd/cg/queryCgExampleById/2
    public Message queryCgExample(@PathVariable String id){
        CgExampleEntity entity = this.cgExampleServiceImpl.queryCgExampleById(id);
        return Message.success(entity);
    }

    @ResponseBody
    @RequestMapping("/queryCgExampleEqByPage")
    // url : http://127.0.0.1:8899/thd/cg/queryCgExampleEqByPage
    public Message queryCgExampleEqByPage(@RequestBody CgExampleEntity entity){
        PageInfo pi = this.cgExampleServiceImpl.queryListEqByPage(entity);
        return Message.success(pi);
    }


    @ResponseBody
    @RequestMapping("/queryCgExampleLikeByPage")
    // url : http://127.0.0.1:8899/thd/cg/queryCgExampleLikeByPage
    public Message queryCgExampleLikeByPage(@RequestBody CgExampleEntity entity){
        PageInfo pi = this.cgExampleServiceImpl.queryListLikeByPage(entity);
        return Message.success(pi);
    }





    @ResponseBody
    @RequestMapping("/insertCgExampleBatch")
    // url : http://127.0.0.1:8899/thd/cg/insertCgExampleBatch
    public Message insertCgExampleBatch(){

        List<CgExampleEntity> l = new ArrayList<CgExampleEntity>();
        for(int i = 0 , j = 10 ; i < j ; i++){
            CgExampleEntity entity = new CgExampleEntity();
            entity.setId("id_" + i );
            entity.setUserAge(i);
            entity.setUserBirthday(new Date());
            entity.setUserName("devil13th_" + i);
            l.add(entity);

        }
        this.cgExampleServiceImpl.insertBatch(l);
        return Message.success("Success");
    }


    @RequestMapping("/queryMyBatisPlusSelectOne/{id}")
    @ResponseBody
    public Message<CgExampleEntity> queryMyBatisPlusSelectOne(@PathVariable("id") String id){
        CgExampleEntity obj = this.cgExampleServiceImpl.queryMyBatisPlusSelectOne(id);
        return Message.success(obj);
    }

    @RequestMapping("/queryByMyBatisPlusSelectList")
    @ResponseBody
    public Message<CgExampleEntity> queryByMyBatisPlusSelectList(){
        LambdaQueryWrapper<CgExampleEntity> lambdaQueryWrapper = new LambdaQueryWrapper<CgExampleEntity>();
        lambdaQueryWrapper.like(CgExampleEntity::getUserName,"a");
        List<CgExampleEntity> result = this.cgExampleMapper.selectList(lambdaQueryWrapper);

        System.out.println(JacksonUtil.objToJson(result));


        QueryWrapper<CgExampleEntity> queryWrapper = new QueryWrapper<CgExampleEntity>();
        queryWrapper.like("user_name","a");
        List<CgExampleEntity> result2 = this.cgExampleMapper.selectList(queryWrapper);

        System.out.println(JacksonUtil.objToJson(result2));

        return Message.success(result);
    }


    @RequestMapping("/testMyBatisPlugs")
    @ResponseBody
    public Message<CgExampleEntity> testMyBatisPlugs(){


        // ======================= 保存 =========================

        CgExampleEntity entity = new CgExampleEntity();
        entity.setId("zhangsan_" + Math.random());
        entity.setUserAge(5);
        entity.setUserName("张三");
        entity.setUserBirthday(new Date());
        entity.setIsDeleted(0);
        this.cgExampleMapper.insert(entity);

        // ======================= 查询 =========================
        LambdaQueryWrapper<CgExampleEntity> lambdaQueryWrapper = new LambdaQueryWrapper<CgExampleEntity>();
        lambdaQueryWrapper.like(CgExampleEntity::getUserName,"张");
        List<CgExampleEntity> result = this.cgExampleMapper.selectList(lambdaQueryWrapper);

        System.out.println(JacksonUtil.objToJson(result));

        // ======================= 修改 =========================
        entity.setUserName("李四xxx");
        this.cgExampleMapper.updateById(entity);


        QueryWrapper<CgExampleEntity> queryWrapper = new QueryWrapper<CgExampleEntity>();
        queryWrapper.like("user_name","李");
        List<CgExampleEntity> result2 = this.cgExampleMapper.selectList(queryWrapper);

        System.out.println(JacksonUtil.objToJson(result2));


        // ======================== 分页查询  ===========================

        IPage<CgExampleEntity> cgExamplePage = new Page<CgExampleEntity>(1, 5);//参数一是当前页，参数二是每页个数

        queryWrapper.orderByAsc("user_name");

        cgExamplePage = this.cgExampleMapper.selectPage(cgExamplePage,queryWrapper);
        List<CgExampleEntity> l = cgExamplePage.getRecords();
        System.out.println(JacksonUtil.objToJson(l));

        return Message.success(cgExamplePage);
    }

    @GetMapping(value="/testResponseBodyHandler")
    @ResponseBody
    public CgExampleEntity testResponseBodyHandler(){
        CgExampleEntity result = new CgExampleEntity();
        result.setId("12341234");
        result.setUserName("devil13th");
        return result;
    }

    @GetMapping(value="/testResponseBodyHandlerString")
    @ResponseBody
    public String testResponseBodyHandlerString(){
        return CommonConstants.STATUS_SUCCESS;
    }











}
